#ifndef __STERPER_H__
#define __STERPER_H__

#include "MC_include.h"
#include "stm32f4xx_hal.h"


/* 步进电机结构体 */
typedef struct
{
    uint16_t pul_pin;                     //电机脉冲输出引脚号
    uint16_t dir_pin;                     //电机方向引脚号
    uint32_t pul_channel;                 //电机脉冲输出通道
    GPIO_TypeDef *pul_port;               //电机脉冲引脚端口
    GPIO_TypeDef *dir_port;               //电机方向引脚端口
    uint16_t oc_pulse_num;                //输出比较计数值，值越小电机转速越快
} Stepper_TypeDef;

/*宏定义*/
/*******************************************************/
#define MOTOR_PUL_TIM                        TIM1
#define MOTOR_PUL_CLK_ENABLE()  		     __TIM1_CLK_ENABLE()
#define MOTOR_PUL_GPIO_AF                    GPIO_AF1_TIM1

#define MOTOR_PUL_IRQn                       TIM1_UP_TIM10_IRQn
#define MOTOR_PUL_IRQHandler                 TIM1_UP_TIM10_IRQHandler

#define ZMOTOR_PUL_TIM                       TIM2
#define ZMOTOR_PUL_CLK_ENABLE()  		     __TIM2_CLK_ENABLE()
#define ZMOTOR_PUL_GPIO_AF                   GPIO_AF1_TIM2

#define ZMOTOR_PUL_IRQn                      TIM2_IRQn
#define ZMOTOR_PUL_IRQHandler                TIM2_IRQHandler

//通道1
//Motor 方向 PB0
#define Z_MOTOR_DIR_PIN                  	 GPIO_PIN_0
#define Z_MOTOR_DIR_GPIO_PORT            	 GPIOB
#define Z_MOTOR_DIR_GPIO_CLK_ENABLE()   	 __HAL_RCC_GPIOB_CLK_ENABLE()

//Motor 脉冲 PA8
#define Z_MOTOR_PUL_PORT       			     GPIOA
#define Z_MOTOR_PUL_PIN             		 GPIO_PIN_1
#define Z_MOTOR_PUL_GPIO_CLK_ENABLE()		 __HAL_RCC_GPIOA_CLK_ENABLE()

//定时器通道
#define Z_MOTOR_PUL_CHANNEL                 TIM_CHANNEL_2

//通道2
//Motor 方向 PB3
#define X_MOTOR_DIR_PIN                  	GPIO_PIN_3
#define X_MOTOR_DIR_GPIO_PORT            	GPIOB
#define X_MOTOR_DIR_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()

//Motor 脉冲 PA9
#define X_MOTOR_PUL_PORT       			    GPIOA
#define X_MOTOR_PUL_PIN             		GPIO_PIN_9
#define X_MOTOR_PUL_GPIO_CLK_ENABLE()	  	__HAL_RCC_GPIOA_CLK_ENABLE()

//定时器通道
#define X_MOTOR_PUL_CHANNEL                 TIM_CHANNEL_2

//通道3
//Motor 方向 PB8
#define Y_MOTOR_DIR_PIN                  	GPIO_PIN_8
#define Y_MOTOR_DIR_GPIO_PORT            	GPIOB
#define Y_MOTOR_DIR_GPIO_CLK_ENABLE()   	__HAL_RCC_GPIOB_CLK_ENABLE()

//Motor 脉冲 PA10
#define Y_MOTOR_PUL_PORT       			    GPIOA
#define Y_MOTOR_PUL_PIN             		GPIO_PIN_10
#define Y_MOTOR_PUL_GPIO_CLK_ENABLE()		__HAL_RCC_GPIOA_CLK_ENABLE()

//定时器通道
#define Y_MOTOR_PUL_CHANNEL                 TIM_CHANNEL_3

//控制使能引脚
/* 带参宏，可以像内联函数一样使用 */
#define MOTOR_PUL(port, pin, x)              HAL_GPIO_WritePin(port, pin, x)
#define MOTOR_DIR(port, pin, x)          	 HAL_GPIO_WritePin(port, pin, x)
#define MOTOR_START(tim, channel, status)    TIM_CCxChannelCmd(tim, channel, status)
#define MOTOR_STOP(tim, channel, status)     TIM_CCxChannelCmd(tim, channel, status)


/*频率相关参数*/
//定时器实际时钟频率为：180MHz/TIM_PRESCALER
//其中 高级定时器的 频率为168MHz,其他定时器为84MHz
//168/TIM_PRESCALER=2MHz
//具体需要的频率可以自己计算
#define TIM_PRESCALER                90
// 定义定时器周期，输出比较模式周期设置为0xFFFF
#define TIM_PERIOD                   0xFFFF

/************************************************************/
#define HIGH GPIO_PIN_SET	  //高电平
#define LOW  GPIO_PIN_RESET	//低电平

#define ON   LOW	          //开
#define OFF  HIGH	          //关

#define CW   HIGH		        //顺时针
#define CCW  LOW      	    //逆时针

/********插补算法相关*********/
/* 坐标轴枚举 */
typedef enum{
  x_axis = 0U,
  y_axis,
  z_axis
}Axis_TypeDef;

typedef enum{
	liner = 0U,
	circular
}InterpolationMode;
/* 直线插补参数结构体 */
typedef struct{
  __IO int32_t endpoint_x;           //终点坐标X
  __IO int32_t endpoint_y;           //终点坐标Y
  __IO int32_t endpoint_pulse;       //到达终点位置需要的脉冲数
  __IO int32_t active_axis;          //当前运动的轴
  __IO int32_t deviation;             //偏差参数
  __IO uint8_t motionstatus : 1;      //插补运动状态
  __IO uint8_t dir_x : 1;             //X轴运动方向
  __IO uint8_t dir_y : 1;             //Y轴运动方向
}LinearInterpolation_TypeDef;

/* 坐标轴象限枚举 */
typedef enum{
  quadrant_1st = 0U,
  quadrant_2nd,
  quadrant_3rd,
  quadrant_4th
}Quadrant_TypeDef;

/* 圆弧插补参数结构体 */
typedef struct{
  __IO int32_t startpoint[2];        //起点坐标X、Y
  __IO int32_t endpoint_x;           //终点坐标X
  __IO int32_t endpoint_y;           //终点坐标Y
  __IO int32_t endpoint_pulse;      //到达终点位置需要的脉冲数
  __IO int32_t active_axis;         //当前运动的轴
  __IO int32_t deviation;            //偏差参数F
  __IO int8_t devi_sign[2];          //偏差方程的运算符号，正负
  __IO uint8_t motionstatus : 1;     //插补运动状态
  __IO uint8_t dir_x : 1;            //X轴运动方向
  __IO uint8_t dir_y : 1;            //Y轴运动方向
  __IO uint8_t dir_interpo : 1;      //插补整体运动方向
  __IO uint8_t crood_pos : 2;        //起点坐标所在的象限
  __IO uint8_t speed;
  __IO uint8_t dir;
}CircularInterpolation_TypeDef;


/* 步进电机数组 */
extern Stepper_TypeDef step_motor[3];
extern LinearInterpolation_TypeDef interpolation_para;
extern CircularInterpolation_TypeDef circular_para;
extern int z_axis_pul;
extern uint8_t z_axis_outStatus;
extern uint8_t interpolationMode;
extern Axis_TypeDef axis;

/*****************************/

void stepper_Init(void);
void Linear_Interpolation(int32_t coordi_x, int32_t coordi_y, uint16_t speed);
uint8_t Circular_InterPolation(int32_t start_x, int32_t start_y, int32_t stop_x, int32_t stop_y, uint16_t speed, uint8_t dir);
void Z_axis_output(int32_t coordi_z, uint16_t speed);
#endif
